package com.bq.gametrade.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bq.gametrade.HttpLink;
import com.bq.gametrade.R;
import com.bq.gametrade.base.BaseActivity;
import com.bq.gametrade.http.VRHttp;
import com.bq.gametrade.http.VRHttpListener;
import com.bq.gametrade.ui.dialog.ExitLoginDialog;
import com.bq.gametrade.ui.dialog.UpdateAppDialog;
import com.bq.gametrade.utils.PasswordHelp;
import com.bq.gametrade.utils.SharedPreferencesUtils;
import com.bq.gametrade.utils.SystemUtils;

import org.json.JSONException;
import org.json.JSONObject;

public class SettingActivity extends BaseActivity implements View.OnClickListener,ExitLoginDialog.OnExitLoginListener{

    private TextView titleName;
    private ImageView imgService,imgBack;
    private RelativeLayout layPersonalData,laySettingPayPassword,layForgetPayPassword,layChangePayPassword,layChangePassword;
    private RelativeLayout layCheckUpdate;
    private Button btnExit;
    private TextView tvVersionCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        initView();
    }

    private void initView(){
        titleName = serchViewById(R.id.title_name);
        titleName.setText("设置");
        imgService = serchViewById(R.id.iv_userService);
        imgService.setOnClickListener(this);
        imgBack = serchViewById(R.id.image_back);
        imgBack.setOnClickListener(this);

        layChangePassword = serchViewById(R.id.lay_changePassword);
        layChangePassword.setOnClickListener(this);
        layCheckUpdate = serchViewById(R.id.lay_checkUpdate);
        layCheckUpdate.setOnClickListener(this);
        btnExit = serchViewById(R.id.btn_exit);
        btnExit.setOnClickListener(this);
        tvVersionCode = serchViewById(R.id.tv_versionCode);
        tvVersionCode.setText("V"+SystemUtils.getVersionName(SettingActivity.this));

    }

    @Override
    protected void onResume() {
        super.onResume();
        setPayPassword();
    }

    private void setPayPassword(){
        layPersonalData = serchViewById(R.id.lay_personalData);
        layPersonalData.setOnClickListener(this);
        laySettingPayPassword = serchViewById(R.id.lay_settingPayPassword);
        laySettingPayPassword.setOnClickListener(this);
        layChangePayPassword = serchViewById(R.id.lay_changePayPassword);
        layChangePayPassword.setOnClickListener(this);
        layForgetPayPassword = serchViewById(R.id.lay_forgetPayPassword);
        layForgetPayPassword.setOnClickListener(this);
        if(application.isLogin()){
            layChangePayPassword.setVisibility(View.VISIBLE);

            layPersonalData.setVisibility(View.VISIBLE);
            if(application.getUserInfo().pay_pass.equals("0")){
                laySettingPayPassword.setVisibility(View.VISIBLE);
                layForgetPayPassword.setVisibility(View.GONE);
            }else if(application.getUserInfo().pay_pass.equals("1")){
                laySettingPayPassword.setVisibility(View.GONE);
                layForgetPayPassword.setVisibility(View.VISIBLE);
            }
        }else {
            layPersonalData.setVisibility(View.GONE);
            laySettingPayPassword.setVisibility(View.GONE);
            layChangePayPassword.setVisibility(View.GONE);
            layForgetPayPassword.setVisibility(View.GONE);
            layChangePassword.setVisibility(View.GONE);
            btnExit.setVisibility(View.GONE);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_userService:

                break;
            case R.id.image_back:
               finish();
                break;
            case R.id.lay_personalData:
                startActivity(new Intent(SettingActivity.this,PersonalDataActivity.class));
                break;
            case R.id.lay_settingPayPassword:
                startActivity(new Intent(SettingActivity.this,SetPayPasswordActivity.class));
                break;
            case R.id.lay_changePayPassword:
                if(application.getUserInfo().pay_pass.equals("0")){
                    startActivity(new Intent(SettingActivity.this,SetPayPasswordActivity.class));
                }else {
                    startActivity(new Intent(SettingActivity.this,ValidationPayPasswordActivity.class));
                }

                break;
            case R.id.lay_forgetPayPassword:
                startActivity(new Intent(SettingActivity.this,ForgetPayPasswordActivity.class));
                break;
            case R.id.lay_changePassword:
                startActivity(new Intent(SettingActivity.this,RetrievePasswordActivity.class));
                break;
            case R.id.lay_checkUpdate:
                updateAppVersion();
                break;
            case R.id.btn_exit:
                ExitLoginDialog dialog = new ExitLoginDialog();
                dialog.show(getSupportFragmentManager(),"dialog");
                break;
        }
    }


    @Override
    public void onExitClick() {
        application.setUserInfo(null);
        SharedPreferencesUtils.saveValue(SettingActivity.this,SharedPreferencesUtils.user_account,"");
        SharedPreferencesUtils.saveValue(SettingActivity.this,SharedPreferencesUtils.user_passwor,"");
        SharedPreferencesUtils.saveValue(SettingActivity.this,SharedPreferencesUtils.get_address,false);
        PasswordHelp.clearUserInfo(SettingActivity.this);
        startActivity(new Intent(SettingActivity.this,MainActivity.class));
    }

    private void updateAppVersion(){
        final int appVersionCode = SystemUtils.getVersionCode(SettingActivity.this);
        showSpotsDialog();
        VRHttp.getResponse(SettingActivity.this, HttpLink.GET_APPVERSION_URL, null, new VRHttpListener() {
            @Override
            public void onSuccess(Object response, boolean isCache) {
                try {
                    JSONObject object = new JSONObject(response.toString());
                    JSONObject jsonObject = object.getJSONObject("android");
                    if(jsonObject != null){
                        boolean isForcedUpdate = jsonObject.getBoolean("forceupdate");
                        String updateContent = jsonObject.getString("text");
                        if(Integer.parseInt(jsonObject.getString("verCode"))>appVersionCode){
                            UpdateAppDialog dialog = new UpdateAppDialog();
                            Bundle bundle = new Bundle();
                            bundle.putString("updateAppUrl",HttpLink.APP_DOWNLOAD_URL);
                            bundle.putBoolean("isForcedUpdate",isForcedUpdate);
                            bundle.putString("text",updateContent);
                            dialog.setArguments(bundle);
                            dialog.show(getSupportFragmentManager(),"");

                        }else {
                            toastShow("当前已是最新版本");
                        }

                    }
                }catch (JSONException e){
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(String error) {
                super.onError(error);
                toastShow("获取当前版本失败");
            }

            @Override
            public void onFinish() {
                super.onFinish();
                closeSpotsDialog();
            }
        });
    }


}
